package com.isunland.managesystem.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.isunland.managesystem.R;
import com.isunland.managesystem.entity.MyPlanProgress;
import java.util.List;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: HuXiaoDan
 * 创建时间: 2017/5/2 10:14
 */
public class MyPlanProgressListAdapter extends ArrayAdapter<MyPlanProgress> {

  /**
   * ViewHolder class for layout.<br />
   * <br />
   * Auto-created on 2015-12-15 16:38:59 by Android Layout Finder
   * (http://www.buzzingandroid.com/tools/android-layout-finder)
   */
  private static class ViewHolder {
    public final LinearLayout rootView;
    final TextView tvWorkTimeTaskCollect;
    final TextView tvTimeAttendanceSummary;
    final TextView tvSinInAttendanceSummary;
    final TextView tvSinOutAttendanceSummary;
    final TextView tvNameTaskCollect;

    private ViewHolder(LinearLayout rootView, TextView tvWorkTimeTaskCollect,
        TextView tvTimeAttendanceSummary, TextView tvSinInAttendanceSummary,
        TextView tvSinOutAttendanceSummary, TextView tvNameTaskCollect) {
      this.rootView = rootView;
      this.tvWorkTimeTaskCollect = tvWorkTimeTaskCollect;
      this.tvTimeAttendanceSummary = tvTimeAttendanceSummary;
      this.tvSinInAttendanceSummary = tvSinInAttendanceSummary;
      this.tvSinOutAttendanceSummary = tvSinOutAttendanceSummary;
      this.tvNameTaskCollect = tvNameTaskCollect;
    }

    public static ViewHolder create(LinearLayout rootView) {
      TextView tvRateCurrentTaskCollect =
          (TextView) rootView.findViewById(R.id.tv_workTime_taskCollect);
      TextView tvTimeAttendanceSummary =
          (TextView) rootView.findViewById(R.id.tv_time_attendanceSummary);
      TextView tvSinInAttendanceSummary =
          (TextView) rootView.findViewById(R.id.tv_sinIn_attendanceSummary);
      TextView tvSinOutAttendanceSummary =
          (TextView) rootView.findViewById(R.id.tv_sinOut_attendanceSummary);
      TextView tvNameTaskCollect = (TextView) rootView.findViewById(R.id.tv_name_taskCollect);
      return new ViewHolder(rootView, tvRateCurrentTaskCollect, tvTimeAttendanceSummary,
          tvSinInAttendanceSummary, tvSinOutAttendanceSummary, tvNameTaskCollect);
    }
  }

  @NonNull @Override
  public View getView(int position, View convertView, @NonNull ViewGroup parent) {
    final ViewHolder vh;
    if (convertView == null) {
      View view = mInflater.inflate(R.layout.adapter_plan_manage, parent, false);
      vh = ViewHolder.create((LinearLayout) view);
      view.setTag(vh);
    } else {
      vh = (ViewHolder) convertView.getTag();
    }

    MyPlanProgress item = getItem(position);
    vh.rootView.setBackgroundColor(Color.WHITE);
    //进展描述
    vh.tvNameTaskCollect.setText(item != null ? item.getExcDesc() : null);
    //任务描述
    vh.tvSinInAttendanceSummary.setVisibility(View.GONE);

    //完成度
    if (TextUtils.isEmpty(item != null ? item.getComprate() : null)) {
      vh.tvSinOutAttendanceSummary.setText("0%");
    } else {
      vh.tvSinOutAttendanceSummary.setText(String.format("%s%%", item.getComprate()));
    }
    //登记时间
    vh.tvWorkTimeTaskCollect.setText(item != null ? item.getRegDate() : "");
    //是否完成
    vh.tvTimeAttendanceSummary.setText(item != null ? item.getIfend() : "");

    //if ("0".equalsIgnoreCase(item.getIfend())) {
    //  vh.tvTimeAttendanceSummary.setText("未完成");
    //}
    //if ("1".equalsIgnoreCase(item.getIfend())) {
    //  vh.tvTimeAttendanceSummary.setText("已完成");
    //}

    return vh.rootView;
  }

  private LayoutInflater mInflater;

  // Constructors
  public MyPlanProgressListAdapter(Context context, List<MyPlanProgress> objects) {
    super(context, 0, objects);
    this.mInflater = LayoutInflater.from(context);
  }
}
